package com.bawei.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bawei.admin.feign.DoctorFeignService;
import com.bawei.common.utils.RedisConstant;
import com.bawei.common.vo.GradeMarkVo;
import com.bawei.common.vo.HospitalDoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Going implements ApplicationRunner {

    @Autowired
    private DoctorFeignService gradeMarkFeignService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        addRedis();
        addDoctorRedis();
    }

    public void addRedis(){
        stringRedisTemplate.opsForValue().set(RedisConstant.GRADEMARKREDISNAME, JSON.toJSONString(gradeMarkFeignService.listGraedMark()));
        System.out.println("执行redis存储");
    }

    public List<GradeMarkVo> getRedisGeaedMark(){
        String s = stringRedisTemplate.opsForValue().get(RedisConstant.GRADEMARKREDISNAME);
        //获取从redis获取到医院的信息和科室的信息
        List<GradeMarkVo> gradeMarkVos = JSON.parseObject(s, new TypeReference<List<GradeMarkVo>>() {
        });
        return gradeMarkVos;
    }

    public void addDoctorRedis(){
        List<HospitalDoctorVo> hospitalDoctorVos = gradeMarkFeignService.listDoctors();
        stringRedisTemplate.opsForValue().set(RedisConstant.DoctorREDISNAME,JSON.toJSONString(hospitalDoctorVos));
        System.out.println("添加医生redis");

    }

    public List<HospitalDoctorVo>  getDoctorRedis(){
        String s = stringRedisTemplate.opsForValue().get(RedisConstant.DoctorREDISNAME);
        List<HospitalDoctorVo> hospitalDoctorVos = JSON.parseObject(s, new TypeReference<List<HospitalDoctorVo>>() {
        });
        return hospitalDoctorVos;
    }


}
